package com.example.stream.demostream.controller;

import com.example.stream.demostream.entity.vo.CredentialVo;
import com.example.stream.demostream.service.ILoginService;
import com.zhe.log.annotation.Log;
import com.zhe.log.enums.LogType;
import com.zhe.shiro.domain.TokenDto;
import com.zhe.web.common.ApiResponse;
import com.zhe.web.exception.BizException;
import com.zhe.web.utils.EmptyChecker;
import com.zhe.web.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: author
 * @date: 2021/3/25
 */
@RestController
@RequestMapping("/auth")
@Api(value = "用户登陆认证Controller", tags = "用户登陆认证Controller")
public class AuthController {

    @Autowired
    private ILoginService loginService;

    @ApiOperation(value = "登陆认证", notes = "返回Token")
    @PostMapping("/login")
    public ApiResponse<TokenDto> login(HttpServletRequest request,
                                       @RequestBody CredentialVo vo, HttpSession session) {
        if (EmptyChecker.isEmpty(vo) || EmptyChecker.isEmpty(vo.getLoginType())) {
            throw new BizException("所少请求参数");
        }

        // 校验证手机号码是否正确
        if (StringUtils.isEmpty(vo.getIdentifier()) || !ValidateUtils.isMobileNo(vo.getIdentifier())) {
            throw new BizException("手机格式错误");
        }

        // 获取登录处理器
        TokenDto tokenDto = loginService.login(vo);


//        测试SpringSession共享
        session.setAttribute("loginUser",tokenDto);
        return ApiResponse.success(tokenDto);
    }

}
